import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api/index.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 保存选中的项目
    pro: null,
    envs: [],
    tasks: [],
    modules: [],
    interfaces: [],
    caseId: null,
  },
  mutations: {
    updatePro(state, val) {
      state.pro = val
    },
    updateEnv(state, val) {
      state.envs = val
    },
    updateTask(state, val) {
      state.tasks = val
    },
    updateModules(state, val) {
      state.modules = val
    },
    updateInterFace(state, val) {
      state.interfaces = val
    },
    updatecaseId(state, val) {
      state.caseId = val
    },
  },
  actions: {
    async getEnvs(context) {
      const response = await api.getEnvs(context.state.pro.id)
      response.data.results.forEach((element) => {
        if (typeof element.db_config === 'string') {
          element.db_config = JSON.parse(element.db_config)
        }
      })
      context.commit('updateEnv', response.data.results)
    },
    async getTask(context) {
      const response = await api.getTask(context.state.pro.id)
      context.commit('updateTask', response.data.results)
    },
    async getModules(context) {
      const response = await api.getModules(context.state.pro.id)
      context.commit('updateModules', response.data.results)
    },
    // 获取项目下所有接口
    async getAllInter(context) {
      const response = await api.getInterFace(context.state.pro.id)
      context.commit('updateInterFace', response.data.results)
    },
  },
  modules: {},
})
